package org.nulldevice.tankshot.entities;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.geom.Area;

import org.nulldevice.tankshot.images.AnimationRepresentant;
import org.nulldevice.tankshot.images.ImageContainer;
import org.nulldevice.tankshot.images.ImageRepresentant;

public abstract class Item {

	protected int posX, posY, sizeX, sizeY;
	protected boolean outOfRange = false;
	protected boolean aboutToExplode = false;
	protected boolean destroyed = false;
	protected String name;
	protected long explodeStart = 0;
	protected boolean recalcBorder = false;
	protected Polygon border;
	protected ImageRepresentant imagerepresentant;
	protected AnimationRepresentant explosionrepresentant;
	double angle;

	public String getName() {
		return name;
	}

	protected int hitpoints = -1;

	public Item(int posX, int posY, int sizeX, int sizeY, String name,
			String ref, int offsetX, int offsetY) {
		this.posX = posX;
		this.posY = posY;
		this.sizeX = sizeX;
		this.sizeY = sizeY;
		this.name = name;
		this.imagerepresentant = ImageContainer.get().getImagerepresentant(ref,
				offsetX, offsetY);
		this.explosionrepresentant = ImageContainer.get()
				.getAnimationrepresentant("explosion.gif", 500, false);
	}

	public int getX() {
		return posX;
	}

	public int getY() {
		return posY;
	}

	public int getSizeX() {
		return sizeX;
	}

	public int getSizeY() {
		return sizeY;
	}

	public void setOutOfRange() {
		outOfRange = true;
	}

	public boolean isDestroyed() {
		return destroyed;
	}

	public void setDestroyed(boolean destroyed) {
		this.destroyed = destroyed;
	}

	public boolean isAboutToExplode() {
		return aboutToExplode;
	}

	public void setAboutToExplode(boolean explode) {
		this.aboutToExplode = explode;
	}

	// return if there was movement or not
	public abstract boolean move(double delta);

	// return Border for collision detection
	public abstract Polygon getBorder();

	// return if the item collides with the other item
	public boolean CollidesWith(Item otherItem) {
		// TODO we should copy the values instead of using the complete border
		boolean collision = false;

		Polygon otherBorder = otherItem.getBorder();

		if (otherBorder.getBounds().intersects(getBorder().getBounds())) {
			// area checking is very slow, so we only check, when the
			// bounding box collides
			Area a = new Area(otherBorder);
			Area b = new Area(getBorder());
			a.intersect(b);
			collision = !a.isEmpty();
		}
		return collision;
	}

	public void explode(double delta) {
		if (explodeStart == 0) {
			explodeStart = System.currentTimeMillis();
		}

		if (System.currentTimeMillis() - explodeStart > 1000) {
			destroyed = true;
		}
	}

	public void hit() {
		if (hitpoints >= 0) {
			hitpoints--;
		}
		System.out.println("hitpoints: " + hitpoints);
		if (hitpoints <= 0) {
			aboutToExplode = true;
		}
	}

	public abstract void hitItem(Item item);

	protected long explosionStartTime = 0;

	public void draw(Graphics g) {

		if (aboutToExplode) {
			if (explosionStartTime == 0) {
				explosionStartTime = System.currentTimeMillis();
			}
			explosionrepresentant.draw(g, posX, posY, angle, System
					.currentTimeMillis()
					- explosionStartTime);
		} else {
			imagerepresentant.draw(g, posX, posY, angle);
		}

		g.setColor(Color.red);

		g.drawPolygon(getBorder());
	}

}
